package org.nanhill.support.jmx.dwjmxservice;

import java.io.IOException;

import javax.management.Descriptor;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.modelmbean.DescriptorSupport;
import javax.management.modelmbean.ModelMBeanAttributeInfo;
import javax.management.modelmbean.ModelMBeanConstructorInfo;
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.ModelMBeanInfoSupport;
import javax.management.modelmbean.ModelMBeanOperationInfo;
import javax.management.modelmbean.RequiredModelMBean;

import com.sun.jdmk.TraceManager;
import com.sun.jdmk.comm.HtmlAdaptorServer;



public class BaseModAgent {


    public BaseModAgent() {

    }

    public  void startAgent(Object inManagedObj) {

	try {
	    TraceManager.parseTraceProperties();
	}
	catch (IOException e) {
	    e.printStackTrace();
	}

	System.out.println("Creating the MBeanServer...");
	MBeanServer server = MBeanServerFactory.createMBeanServer();

     // wrap our object-to-be-managed with a pre-fab modelbean
      try {
        ObjectName tpMBeanName = new ObjectName("MBean:name=ClickMeter");
        RequiredModelMBean modelmbean = new RequiredModelMBean(createMBeanInfo());
        modelmbean.setManagedResource(inManagedObj, "objectReference");
        server.registerMBean(modelmbean, tpMBeanName);
      } catch (Exception e) {
         System.out.println("Cannot register ClickMeter MBean!");
         e.printStackTrace();
         return;
      }
	System.out.println("Create a new HTML protocol adaptor...");
	HtmlAdaptorServer hadaptor = new HtmlAdaptorServer();
	ObjectName adaptorName = null;
	try {
	    adaptorName = new ObjectName("Adaptor:name=hadaptor,port=8082");

	    server.registerMBean(hadaptor, adaptorName);
	} catch(Exception e) {
	    System.out.println("Cannot create HTML protocol adaptor !");
	    e.printStackTrace();
	    return;
	}
	hadaptor.start();
    }

    private ModelMBeanInfo createMBeanInfo() {
		Descriptor atDesc = new DescriptorSupport(new String[] {
                 "name=PanelValue",
                 "descriptorType=attribute",
                 "default=0",
                 "displayName=Value of the Panel",
                 "getMethod=getPanelValue",
                 "setMethod=setPanelValue"
                }
                );

    ModelMBeanAttributeInfo [] mmbai = new ModelMBeanAttributeInfo[1];
    mmbai[0] = new ModelMBeanAttributeInfo("PanelValue","java.lang.Integer",
      "The ClickMeter Value",  true,true, false, atDesc);

    ModelMBeanOperationInfo [] mmboi = new ModelMBeanOperationInfo[4];

    mmboi[0] = new ModelMBeanOperationInfo("incPanelValue", "increment the meter value",
    null, "void", ModelMBeanOperationInfo.ACTION
    );


    mmboi[1] = new ModelMBeanOperationInfo("decPanelValue", "decrement the meter value",
    null, "void", ModelMBeanOperationInfo.ACTION
    );



    // define the getter and setter for attributes



    mmboi[2] = new ModelMBeanOperationInfo("getPanelValue", "getter for PanelValue",
     null,"Integer", ModelMBeanOperationInfo.INFO);



    MBeanParameterInfo [] mbpi = new MBeanParameterInfo[1];
    mbpi[0] =  new MBeanParameterInfo("inVal", "java.lang.Integer", "value to set");
    mmboi[3] = new ModelMBeanOperationInfo("setPanelValue","setter for PanelValue",
     mbpi, "void", ModelMBeanOperationInfo.ACTION);





    ModelMBeanConstructorInfo [] mmbci = new ModelMBeanConstructorInfo[1];
    mmbci[0] = new ModelMBeanConstructorInfo("ClickMeterMod", "constructor for Model Bean Sample",
    null);


    return new ModelMBeanInfoSupport("dwjmxservice.basic.ClickMeterMod",
    "dw ModelMBean example", mmbai, mmbci, mmboi, null);
    }

}